# 2021年1月18日星期一 上午8:47 周考2
# 人工智能学院1906A
# 裴云鹏


# 1.用嵌套元组形式将左表数据进行保存。
# 2.用while循环实现右图菜单界面
# 3.实现选项1功能
# 4. 实现选项2功能
# 5. 实现选项3、4功能


xerror_prompt = '输入有误，请重新输入！'
xproducts_arr = (
    ('泡泡糖', 5),
    ('巧克力糖', 38),
    ('Vc糖', 44),
    ('咖啡糖', 31),
    ('水果糖', 16),
    ('薄荷糖', 22)
)
xprompt = '''\
--------糖果商店--------
1.查看糖果
2.购买糖果
3.付款结账
4.退出系统
'''


def input_value(xtype, xprompt, xerror_prompt=xerror_prompt):
    '''
    Common procedure to receive user's input.

    :param xtype: The expected type.
    :param xprompt: The prompt.
    :param xerror_prompt: The prompt when cannot parse user's input as the specified type
    :return: The very value user inputted.
    '''
    while True:
        xinput = input(xprompt).strip()
        try:
            xinput = xtype(xinput)
            if str == xtype and len(xinput) == 0:
                print('不能为空白，请重新输入。')
                continue
            return xinput
        except ValueError:
            print(xerror_prompt)
            continue


def show_products(xproducts_arr):
    '''
    Display detailed information of products in the command line.

    :param xproducts_arr: A nested list representing products
    :return: None
    '''
    xnumber = 1
    for xname, xprice in xproducts_arr:
        print('#{number} {name} {price:.2f}元一件'.format(number=xnumber, name=xname, price=xprice))
        xnumber += 1


def add_to_basket(xnumber, xcnt):
    '''
    Add a product to the user's shopping basket.

    :param xnumber: The number of the product
    :param xcnt: The count of the product that user wish to add to her basket.
    :return: None
    '''
    xidx = xnumber - 1
    if xidx in xbasket_dict:
        xbasket_dict[xidx] += xcnt
    else:
        xbasket_dict[xidx] = xcnt


def show_basket(xbasket_dict):
    '''
    Display the user's shopping basket.

    :param xbasket_dict: The shopping basket
    :return: The money needed in total
    '''
    if 0 == len(xbasket_dict):
        print('您的购物车目前为空。')
        return 0.0
    print('您的购物车目前如下：')
    xsum = 0
    for xnumber, xcnt in xbasket_dict.items():
        xname = xproducts_arr[xnumber - 1][0]
        xprice = xproducts_arr[xnumber - 1][1]
        xmoney = xcnt * xprice
        print('{name} {price:.2f}元一件 共{cnt}件 小计{money:.2f}元'.format(name=xname, price=xprice, cnt=xcnt, money=xmoney))
        xsum += xmoney
    print('您目前消费{:.2f}元'.format(xsum))
    return xsum

xbasket_dict = {}
xpaied = 0.0
while True:
    print(xprompt)
    xchoice = input_value(int, '请选择功能序号：', '选择有误，请重新选择。')
    if not(1 <= xchoice <= 4):
        print('选择有误，请重新选择。')
        continue
    if 1 == xchoice:
        print('现在本店提供糖果如下：')
        show_products(xproducts_arr)
        continue
    elif 2 == xchoice:
        while True:
            show_basket(xbasket_dict)
            xlen = len(xproducts_arr)
            xnumber = input_value(int, '您要哪个编号对应的糖果？：(输入0退出购买。)')
            if 0 == xnumber:
                break
            if not (1 <= xnumber <= xlen):
                print('您输入的编号不存在！')
                continue
            xcnt = input_value(int, '您买几件？：')
            if xcnt <= 0:
                print('您别开玩笑了。')
                continue
            add_to_basket(xnumber, xcnt)
            xname = xproducts_arr[xnumber - 1][0]
            print('您购买了{}件‘{}’'.format(xcnt, xname))
        continue
    elif 3 == xchoice:
        xsum = show_basket(xbasket_dict)
        xlen = len(xbasket_dict)
        if xlen <= 0:
            print('您没有需要结清的账款。')
            continue
        xdiff = xsum - xpaied
        print('您已经付款{:.2f}元，还需要付{:.2f}元。'.format(xpaied, xdiff))
        xpay = input_value(float, '请付款（输入金额）：')
        if xpay <= 0:
            print('您别开玩笑了。')
            continue
        xpaied += xpay
        if xpaied >= xsum:
            print('结账完毕。')
            if xpaied > xsum:
                print('请拿好找零：{:.2f}元'.format(xpaied - xsum))
            xbasket_dict = {}
            xpaied = 0
        else:
            print('您还欠款{:.2f}元'.format(xsum - xpaied))
        continue
    elif 4 == xchoice:
        print('欢迎您再来！')
        break
    break